// Interface for the discovered module information
// import path from 'node:path'; // Removed
// import { URL } from 'node:url'; // Removed
// import { fileURLToPath } from 'node:url'; // Removed

import { log } from '../logger';
// import { toKebabCase } from '../utils/stringUtils'; // Removed for now

export interface DiscoveredModule {
  path: string; // Back to relative path from glob, e.g., ../../api/test/index.ts
  module: Record<string, any>;
  // channelPrefix: string; // Removed
}

/**
 * Scans the api directory for modules relative to this file.
 * @returns An array of objects, each containing the relative path and the loaded module.
 */
export function discoverApiModules(): DiscoveredModule[] {
  // Ensure __filename and __dirname are declared first // Removed
  // const __filename = fileURLToPath(import.meta.url); // Removed
  // const __dirname = path.dirname(__filename); // Removed

  log.info('[ApiLoader] Starting to discover API modules...');
  // log.debug(`[ApiLoader] Current directory: ${__dirname}`); // Removed

  // Define the base API path relative to this loader file // Removed logic
  // const apiDirRelativePath = '../../api'; // Removed
  // Resolve the absolute path to the API directory // Removed
  // const apiDirAbsolutePath = path.resolve(__dirname, apiDirRelativePath); // Removed
  // log.debug(`[ApiLoader] Resolved API directory absolute path: ${apiDirAbsolutePath}`); // Removed

  // Vite import.meta.glob requires a literal string for the pattern.
  const modules = import.meta.glob('../../api/**/*.ts', { eager: true });

  const discoveredModules: DiscoveredModule[] = [];
  const moduleCount = Object.keys(modules).length;
  log.info(`[ApiLoader] Found ${moduleCount} potential API module files via glob.`);

  for (const moduleRelativePath in modules) {
    const moduleContent = modules[moduleRelativePath] as Record<string, any>;
    if (moduleContent) {
      // Get the absolute path of the module // Removed
      // const absolutePath = path.resolve(__dirname, moduleRelativePath); // Removed

      // Calculate the path relative to the API base directory (e.g., 'user/profileSettings.ts') // Removed
      // const pathRelativeToApi = path.relative(apiDirAbsolutePath, absolutePath); // Removed

      // Generate the channel prefix (e.g., 'api/user/profile-settings') // Removed
      // const normalizedPath = pathRelativeToApi.replace(/\\/g, '/'); // Removed
      // const parsedPath = path.parse(normalizedPath); // Removed
      // const dirParts = parsedPath.dir ? parsedPath.dir.split('/') : []; // Removed
      // const filePart = parsedPath.name; // Removed
      // const kebabParts = ['api', ...dirParts, filePart] // Removed
      //   .filter(part => part) // Removed
      //   .map(toKebabCase); // Removed
      // const channelPrefix = kebabParts.join('/'); // Removed

      // Just return the relative path from glob
      discoveredModules.push({ path: moduleRelativePath, module: moduleContent });
      log.debug(`[ApiLoader] Discovered module with relative path: ${moduleRelativePath}`);
    } else {
      log.warn(`[ApiLoader] Failed to load module content for relative path ${moduleRelativePath}`);
    }
  }

  if (discoveredModules.length > 0) {
    log.info(`[ApiLoader] Successfully processed ${discoveredModules.length} API modules.`);
  } else {
    log.info('[ApiLoader] No API modules found in src/main/api/.');
  }
  log.info('[ApiLoader] API module discovery complete.');
  return discoveredModules;
}
